package com.irdstudio.apicenter.console.controller.make;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;

/**
 * 应用授权管理
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-07-22
 */
@Controller
@RequestMapping(value = "/auth")
public class AuthManagerAction extends MacmsBaseAction {

	/**
	 * APP用 户列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appUserList")
	public String appUserList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String userId = (request.getParameter("userId") == null) ? "" : request
				.getParameter("userId");
		String userName = (request.getParameter("userName") == null) ? ""
				: request.getParameter("userName");

		// 将查询条件放入上下文中
		request.setAttribute("userId", userId);
		request.setAttribute("userName", userName);

		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中
		String sql = "select * from app_user where app_id=? and APP_USERID like '%"
				+ userId + "%' and APP_USERNAME like '%" + userName + "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("auList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("auList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);

		return "auth/appUserList";
	}

	/**
	 * 添加用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addUser")
	public String addUser(HttpServletRequest request) {
		String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addUser";
	}

	/**
	 * APP机构列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appOrgList")
	public String appOrgList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String orgId = (request.getParameter("orgId") == null) ? "" : request
				.getParameter("orgId");
		String organName = (request.getParameter("organName") == null) ? ""
				: request.getParameter("organName");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		// 将查询条件放入上下文
		request.setAttribute("orgId", orgId);
		request.setAttribute("organName", organName);

		// 查询数据并放入到上下文中
		String sql = "select * from app_org where app_id=? and ORGID like '%"
				+ orgId + "%' and ORGANNAME like '%" + organName + "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("aoList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("aoList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appOrgList";
	}

	/**
	 * APP角色列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appRoleList")
	public String appRoleList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String appRoleId = (request.getParameter("appRoleId") == null) ? ""
				: request.getParameter("appRoleId");
		String appRoleName = (request.getParameter("appRoleName") == null) ? ""
				: request.getParameter("appRoleName");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		// 将查询条件放入上下文
		request.setAttribute("appRoleId", appRoleId);
		request.setAttribute("appRoleName", appRoleName);

		// 查询数据并放入到上下文中
		String sql = "select * from app_role where app_id=? and APP_ROLE_ID like '%"
				+ appRoleId
				+ "%' and APP_ROLE_NAME like '%"
				+ appRoleName
				+ "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("arList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("arList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appRoleList";
	}

	/**
	 * APP岗位列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appDutyList")
	public String appDutyList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String dutyId = (request.getParameter("dutyId") == null) ? "" : request
				.getParameter("dutyId");
		String dutyName = (request.getParameter("dutyName") == null) ? ""
				: request.getParameter("dutyName");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		// 将查询条件放入上下文中
		request.setAttribute("dutyId", dutyId);
		request.setAttribute("dutyName", dutyName);

		// 查询数据并放入到上下文中
		String sql = "select * from app_duty where app_id=? and APP_DUTY_ID like '%"
				+ dutyId + "%' and APP_DUTY_NAME like '%" + dutyName + "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("adList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("adList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appDutyList";
	}

	/**
	 * APP菜单列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appMenuList")
	public String appMenuList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String menuId = (request.getParameter("menuId") == null) ? "" : request
				.getParameter("menuId");
		String menuName = (request.getParameter("menuName") == null) ? ""
				: request.getParameter("menuName");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		// 将查询调教放入上下文中
		request.setAttribute("menuId", menuId);
		request.setAttribute("menuName", menuName);

		// 查询数据并放入到上下文中
		String sql = "select * from app_menu_resource where app_id=? and MENU_ID like '%"
				+ menuId + "%' and MENU_NAME like '%" + menuName + "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("amrList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("amrList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appMenuList";
	}

	/**
	 * APP角色权限列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appRoleRightList")
	public String appRoleRightList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中
		String sql = "select * from app_role_right where app_id=?";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("arrList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("arrList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);

		return "auth/appRoleRightList";
	}

	/**
	 * APP角色权限列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "setAppRoleRight")
	public String setAppRoleRight(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		return "auth/setAppRoleRight";
	}

	/**
	 * 移动设备列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appDeviceList")
	public String appDeviceList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		String deviceId = (null == request.getParameter("deviceId") ? ""
				: request.getParameter("deviceId"));
		String deviceType = (null == request.getParameter("deviceType") ? ""
				: request.getParameter("deviceType"));
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 将查询条件放入上下文中
		request.setAttribute("deviceId", deviceId);
		request.setAttribute("deviceType", deviceType);

		// 查询数据并放入到上下文中
		String sql = "select * from app_device_register where app_id=?  and device_id  like  '%"
				+ deviceId
				+ "%'   and  device_type  like  '%"
				+ deviceType
				+ "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("adrList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("adrList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appDeviceList";
	}

	/**
	 * 广播轮播配置列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appAdRollList")
	public String appAdRollList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		// 查询数据并放入到上下文中
		String sql = "select * from app_ad_roll where app_id=?";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("adrList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("adrList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appAdRollList";
	}

	/**
	 * 产品信息列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appPrdShowList")
	public String appPrdShowList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		String record_key = request.getParameter("record_key") == null ? "" : request.getParameter("record_key");
		String prd_name = request.getParameter("prd_name") == null ? "" : request.getParameter("prd_name");
		// 将查询条件放入上下文中
		request.setAttribute("recordkey", record_key);
		request.setAttribute("prdname", prd_name);
		// 查询数据并放入到上下文中
		String sql = "select * from app_prd_show where app_id=? and record_key  like  '%"
				+ record_key + "%' and prd_name like '%" + prd_name + "%'";
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("dataList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("dataList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appPrdShowList";
	}

	/**
	 * 商品信息列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appCommodityList")
	public String appCommodityList(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		this.getAllRequestData(request);
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}
		String record_key = request.getParameter("record_key") == null ? "" : request.getParameter("record_key");
		String commodity_name = request.getParameter("commodity_name") == null ? "" : request.getParameter("commodity_name");
		// 将查询条件放入上下文中
		request.setAttribute("recordkey", record_key);
		request.setAttribute("commodityname", commodity_name);
		// 查询数据并放入到上下文中
		String sql = "select * from app_commodity_show where app_id=? and record_key  like  '%"
				+ record_key + "%' and commodity_name like '%" + commodity_name + "%'";
		// 查询数据并放入到上下文中
		PageInfo pageInfo = this.getPageInfo();
		context.addDataList("dataList", this.getTableModelDAO().queryList(sql,
				pageInfo, new Object[] { appId }));
		context.addPageInfo("dataList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);
		request.setAttribute("appid", appId);
		return "auth/appCommodityList";
	}
}
